package com.androider.timememory.activity;

import java.io.File;

import com.androider.timememory.R;
import com.androider.timememory.utils.ActivityUtil;
import com.androider.timememory.utils.FileUtil;
import com.androider.timememory.utils.SMSUtil;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class SendSMS extends Activity {
	private final static int REQUEST_ADD_PEOPLE = 3;
	private final static String SEND_SMS_TAG = " @TimeMemory.您若收到此短信说明您未安装【时光记忆】软件，请下载安装：http://sdfsdf. { %s}";
	private Button positive;
	private Button negative;
	private EditText sendContent;
	private EditText sendPeople;
	private Button addPeople;
	private String number;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.send_sms_layout);
		
		positive = (Button) findViewById(R.id.positive);
		negative = (Button) findViewById(R.id.negative);
		sendContent = (EditText) findViewById(R.id.send_sms_content);
		sendPeople = (EditText) findViewById(R.id.send_sms_people);
		addPeople = (Button) findViewById(R.id.add_people);

		positive.setText(R.string.sms_button_send);
		negative.setText(R.string.sms_button_cancel);
		
		negative.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SendSMS.this.finish();
			}
			
		});
		
		sendContent.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				sendContent.setHint("");
				return false;
			}
			
		});
		
		positive.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = sendContent.getText().toString();
				final String people = sendPeople.getText().toString();
				
				if(people == null || people.equals("")) {
					ActivityUtil.showShortTip(SendSMS.this, R.string.sms_send_no_people);
					return;
				}
				
				if(content == null || content.equals("")) {
					ActivityUtil.showShortTip(SendSMS.this, R.string.sms_send_no_content);
					return;
				}
				
				if(content.length() > 10){
					ActivityUtil.showShortTip(SendSMS.this, R.string.sms_send_content_wrong);
					return;
				}
				content = content.trim();
				number = number.replace("-", "");
				content = String.format(SEND_SMS_TAG, content);
				SMSUtil.SendSMS(number, content, SendSMS.this);
				ActivityUtil.showShortTip(SendSMS.this, R.string.sms_send_success);
				SendSMS.this.finish();

			}
			
		});
		
		addPeople.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_PICK);
				i.setType("vnd.android.cursor.dir/phone");
				startActivityForResult(i, REQUEST_ADD_PEOPLE);
			}
			
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK) {
			switch(requestCode) {
			case REQUEST_ADD_PEOPLE:
				if (data == null) {
					return;
				}
				
				Uri uri = data.getData();
				Cursor cursor = getContentResolver().query(uri, null, null, null,
						null);
				cursor.moveToFirst();
				number = cursor.getString(cursor
						.getColumnIndexOrThrow(Phones.NUMBER));
				final String name = cursor.getString(cursor
						.getColumnIndexOrThrow(Phones.DISPLAY_NAME));
				sendPeople.setText(number + " (" + name + ")");
				
				break;
			}
			
		}
	}

	
}
